package com.woniuxy.educationplatform.mapper;

import com.woniuxy.educationplatform.bean.GoodsBean;
import com.woniuxy.educationplatform.bean.OrderBean;
import com.woniuxy.educationplatform.bean.TransportBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.context.annotation.Bean;

import java.util.List;
import java.util.Map;

@Mapper
public interface OrderMapper {
    /**
     * 根据id查订单
     * @param id
     * @return
     */
    @Results({
            @Result(property = "transportBean",column = "fk_transport_id",javaType = TransportBean.class,one =
            @One(select = "com.woniuxy.educationplatform.mapper.TransportMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,order_no as orderNo,create_time createTime,student_id,student_name as studentName," +
            "telephone,money,address,accept,status,fk_transport_id from order_info where id = #{id}")
    OrderBean findById(Long id);

    /**
     * 新增一个订单
     * @param orderBean
     * @return
     */
    @Insert("insert into order_info (order_no,create_time,student_name,telephone,money," +
            "address,accept,status,fk_transport_id,student_id) values (#{o.orderNo},now(),#{o.studentName}," +
            "#{o.telephone},#{o.money},#{o.address},#{o.accept},#{o.status},#{o.transportBean.id},#{o.studentId})")
    int addOrder(@Param("o") OrderBean orderBean);

    /**
     * 分页查询
     * @param index：起始序号
     * @param nums：每页几条数据
     * @return
     */
    @Results({
            @Result(property = "transportBean",column = "fk_transport_id",javaType = TransportBean.class,one =
            @One(select = "com.woniuxy.educationplatform.mapper.TransportMapper.findById",fetchType = FetchType.LAZY))
    })
    @Select("select id,order_no as orderNo,create_time createTime,student_name as studentName," +
            "telephone,money,address,accept,status,fk_transport_id from order_info limit #{index},#{num}")
    List<OrderBean> findByPage(@Param("index") int index, @Param("num") int nums);

    /**
     * 修改订单
     * @param orderBean
     * @return
     */
    int update(@Param("o") OrderBean orderBean);

    /**
     * 查询属于某学生消费金额及订单数
     * @param id 学生id
     * @return list
     */
    Map<String,Double> findOrderDetailsByStudentId(Long id);

    @Select("select count(id) from order_info")
    int count();

    List<OrderBean> findByCondition(@Param("o") OrderBean o);

    @Select("select g.goods_no as goodsNo,g.goods_name as goodsName,g.postage,g.sales_price as salesPrice," +
            "g.market_price as marketPrice,g.express_type as expressType from goods_info as g, " +
            "order_goods as o where o.goods_id = g.id and o.fk_order_id = #{id}")
    List<GoodsBean> findByOrderId(Long id);

    @Select("select o.nums from order_goods as o where o.fk_order_id = #{id}")
    int[] findNumsByOrderId(Long id);

    @Update("update order_info set money = #{money} where id = #{id}")
    void changeMoney(@Param("id") Long id,@Param("money") Double money);
}
